﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Model
{
    public class VerifycodeRepository
    {
        private JzSportDbContext _dbContext;

        public VerifycodeRepository(JzSportDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public bool Create(Verifycode verifycode)
        {
            _dbContext.Set<Verifycode>().Add(verifycode);
            return false;
        }

        public Verifycode Get(Guid id)
        {
            return _dbContext.Set<Verifycode>().Where(p => p.Id == id).SingleOrDefault();
        }

        public void Update(Guid id, Verifycode verifycode)
        {
            _dbContext.Entry<Verifycode>(verifycode).State = System.Data.EntityState.Modified;
        }

        public bool Delete(Verifycode verifycode)
        {
            _dbContext.Set<Verifycode>().Remove(verifycode);
            return true;
        }

        /// <summary>
        /// 根据电话和用途获取已保存的验证码
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="usage"></param>
        /// <returns></returns>
        public Verifycode GetByPhone(string phone, int usage)
        {
            return _dbContext.Set<Verifycode>().Where(p => p.Phone == phone && p.Usage == usage).SingleOrDefault();
        }
    }
}
